package ar.uba.fi.tonyvaliente.web.manager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import ar.uba.fi.tonyvaliente.commands.CommandResult;
import ar.uba.fi.tonyvaliente.documents.DocumentHashing;
import ar.uba.fi.tonyvaliente.documents.DocumentIndex;
import ar.uba.fi.tonyvaliente.documents.DocumentInfo;
import ar.uba.fi.tonyvaliente.documents.DocumentManager;
import ar.uba.fi.tonyvaliente.documents.QueryDocumentsResult;
import ar.uba.fi.tonyvaliente.signature.HashFunction;
import ar.uba.fi.tonyvaliente.signature.SignatureConfiguration;
import ar.uba.fi.tonyvaliente.signature.SignatureConfigurationException;
import ar.uba.fi.tonyvaliente.signature.process.SignatureGenerator;
import ar.uba.fi.tonyvaliente.utils.RecursosAplicacion;
import ar.uba.fi.tonyvaliente.web.search.DocumentSearchResult;

public class DocumentSearchManager {

	private static final String DOCS_ROOT = RecursosAplicacion.instance().getProperty("docs.root");
	
	private String indexType;

	public DocumentSearchManager(String indexType) {
		this.indexType = indexType;
	}

	public List<DocumentSearchResult> queryIndex(String query) {

		List<DocumentSearchResult> documents = null;
		if (this.indexType.equalsIgnoreCase("B + Tree")) {
			documents = this.queryBPlusTree(query);
		} else if (this.indexType.equalsIgnoreCase("Signature File")) {
			documents = this.querySignatureFile(query);
		}

		return documents;
	}

	private List<DocumentSearchResult> queryBPlusTree(String query) {

		QueryDocumentsResult resultado = DocumentManager.queryDocuments(query,
				true);
		
		List<Integer> ids = resultado.getResults();

		System.out.println("id's obtenidos: " + ids);

		
		List<DocumentSearchResult> documents = new ArrayList<DocumentSearchResult>();
		
		DocumentHashing documentHashing = new DocumentHashing();
		
		for(Integer id : ids) {
			
			DocumentInfo documentInfo = documentHashing.getDocumentInfo(id);
			DocumentSearchResult doc1 = new DocumentSearchResult(id,
					documentInfo.getTitle(), "");
			documents.add(doc1);
		}

		return documents;
	}

	private List<DocumentSearchResult> querySignatureFile(String query) {
		SignatureConfiguration config = null;
		try {
			config = SignatureConfiguration.loadFromFile(RecursosAplicacion
					.instance().getProperty("hash.config"));
		} catch (SignatureConfigurationException e) {
			e.printStackTrace();
			return null;
		}
		ArrayList<HashFunction> hashFunctions = new ArrayList<HashFunction>(
				config.getHashFunctions());
		int length = config.getSignatureLength();
		QueryDocumentsResult resultado = SignatureGenerator.queryDocuments(query,
				true, hashFunctions, length);

		System.out.println(resultado.getResults());

		List<DocumentSearchResult> documents = new ArrayList<DocumentSearchResult>();
		DocumentHashing documentHashing = new DocumentHashing();
		
		for (Integer id : resultado.getResults()) {

			DocumentInfo documentInfo = documentHashing.getDocumentInfo(id);
			DocumentSearchResult doc = new DocumentSearchResult(id,
					documentInfo.getTitle(), "");
			documents.add(doc);
			
		}

		return documents;
	}
	
	public String getDocument(int id){

		DocumentHashing documentHashing = new DocumentHashing();
		
		DocumentInfo documentInfo = documentHashing.getDocumentInfo(id);
		StringBuffer message = new StringBuffer();
		try {
			String path = documentInfo.getHash();
			File file = new File(DOCS_ROOT + "/" + path.substring(0, 1) + "/"
					+ path.substring(1, 2), path);
			if (file.exists()) {
				BufferedReader reader = new BufferedReader(
						new FileReader(file));
				String line;
				while ((line = reader.readLine()) != null) {
					message.append(line);
					message.append("\n");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}


		return message.toString();
		
	}
}
